<template>
  <div>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="标题"></el-input>
    </el-form-item>
    <el-form-item label="摘要" prop="summary">
      <el-input v-model="dataForm.summary" placeholder="摘要"></el-input>
    </el-form-item>


   <!-- <el-form-item label="内容" prop="content">
      <el-input v-model="dataForm.content" placeholder="内容"></el-input>
    </el-form-item>-->
      <div id="toolbar-container"></div>
      <div id="editor-container" style="height: 400px;border: 1px solid #e8e8e8"></div>
   <!-- <el-form-item label="类型id" prop="typeId">
      <el-input v-model="dataForm.typeId" placeholder="类型id"></el-input>
    </el-form-item>
    <el-form-item label="类型名称" prop="type">
      <el-input v-model="dataForm.type" placeholder="类型名称"></el-input>
    </el-form-item>

    <el-form-item label="关键词" prop="keyWord">
      <el-input v-model="dataForm.keyWord" placeholder="关键词"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="state">
      <el-input v-model="dataForm.state" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="是否置顶" prop="stick">
      <el-input v-model="dataForm.stick" placeholder="是否置顶"></el-input>
    </el-form-item>
    <el-form-item label="是否原创" prop="original">
      <el-input v-model="dataForm.original" placeholder="是否原创"></el-input>
    </el-form-item>
    <el-form-item label="缩略图路径" prop="smallImg">
      <el-input v-model="dataForm.smallImg" placeholder="缩略图路径"></el-input>
    </el-form-item>-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </div>
</template>

<script>
  import '@wangeditor/editor/dist/css/style.css'
  import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
  export default {
    components: {Editor,Toolbar},
    data () {
      return {
        visible: false,
        editor: null,
        html: '<p>hello</p>',
        toolbarConfig: { },
        editorConfig: { placeholder: '请输入内容...' },
        mode: 'default', // or 'simple'
        dataForm: {
          id: 0,
          title: '',
          summary: '',
          createTime: '',
          authorId: '',
          author: '',
          content: '',
          typeId: '',
          type: '',
          clickNum: '',
          replyNum: '',
          keyWord: '',
          state: '',
          stick: '',
          original: '',
          smallImg: ''
        },
        dataRule: {
          title: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],
          summary: [
            { required: true, message: '摘要不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '发布时间不能为空', trigger: 'blur' }
          ],
          authorId: [
            { required: true, message: '作者id不能为空', trigger: 'blur' }
          ],
          author: [
            { required: true, message: '作者名称不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '内容不能为空', trigger: 'blur' }
          ],
          typeId: [
            { required: true, message: '类型id不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '类型名称不能为空', trigger: 'blur' }
          ],
          clickNum: [
            { required: true, message: '点击量不能为空', trigger: 'blur' }
          ],
          replyNum: [
            { required: true, message: '评论数不能为空', trigger: 'blur' }
          ],
          keyWord: [
            { required: true, message: '关键词不能为空', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          stick: [
            { required: true, message: '是否置顶不能为空', trigger: 'blur' }
          ],
          original: [
            { required: true, message: '是否原创不能为空', trigger: 'blur' }
          ],
          smallImg: [
            { required: true, message: '缩略图路径不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    mounted() {
      this.$nextTick(()=>{
        //this.initWangEditor();
      })
    },
    beforeDestroy() {
      const editor = this.editor
      if (editor == null) return
      editor.destroy() // 组件销毁时，及时销毁编辑器
    },

    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/blog/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.title = data.blog.title
                this.dataForm.summary = data.blog.summary
                this.dataForm.createTime = data.blog.createTime
                this.dataForm.authorId = data.blog.authorId
                this.dataForm.author = data.blog.author
                this.dataForm.content = data.blog.content
                this.dataForm.typeId = data.blog.typeId
                this.dataForm.type = data.blog.type
                this.dataForm.clickNum = data.blog.clickNum
                this.dataForm.replyNum = data.blog.replyNum
                this.dataForm.keyWord = data.blog.keyWord
                this.dataForm.state = data.blog.state
                this.dataForm.stick = data.blog.stick
                this.dataForm.original = data.blog.original
                this.dataForm.smallImg = data.blog.smallImg
              }
            })
          }
        })
      },
      initWangEditor(){
        const editorConfig= {}
        editorConfig.maxLength = 400
        editorConfig.placeholder = '请输入内容'
        editorConfig.onChange = (e) => {
          // 当编辑器选区、内容变化时，即触发
          console.log('content', e.children)
          console.log('html', e.getHtml())
        }

// 创建编辑器
        let editor = createEditor({
          selector: '#editor-container',
          config: editorConfig,
          mode: 'default' // 或 'simple' 参考下文
        })
        this.editor=editor;
// 创建工具栏
        const toolbar = createToolbar({
          editor,
          selector: '#toolbar-container',
          mode: 'default' // 或 'simple' 参考下文
        })
      },
      onCreated(editor) {
        this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/blog/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'title': this.dataForm.title,
                'summary': this.dataForm.summary,
                'createTime': this.dataForm.createTime,
                'authorId': this.dataForm.authorId,
                'author': this.dataForm.author,
                'content': this.dataForm.content,
                'typeId': this.dataForm.typeId,
                'type': this.dataForm.type,
                'clickNum': this.dataForm.clickNum,
                'replyNum': this.dataForm.replyNum,
                'keyWord': this.dataForm.keyWord,
                'state': this.dataForm.state,
                'stick': this.dataForm.stick,
                'original': this.dataForm.original,
                'smallImg': this.dataForm.smallImg
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
<!--<style src="@wangeditor/editor/dist/css/style.css"></style>-->
